# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
#若收到的项目重复，删除重复的项目数据
#清理、检查和处理项目数据
#将项目数据存入数据库
from scrapy.exceptions import DropItem
import datetime


class MqlDemoPipeline:
    def process_item(self, item, spider):
        if item["replytime"]:
            now=datetime.datetime.now()
            if '天前' in item['replytime']:
                days=int(item["replytime"][0])
                date=now-datetime.timedelta(days=days)
            elif '前天' in item["replytime"]:
                date=now-datetime.timedelta(days=2)
            elif "昨天" in item['replytime'] or '昨晚' in item["replytime"]:
                date=now-datetime.timedelta(days=1)
            elif '小时前' in item["replytime"] or '分钟前' in item["replytime"]:
                date=now
            else:
                date=item['replytime'].split(' ')[0]
                date=datetime.datetime.strftime(date,'%Y-%m-%d')
            date=date.strftime("%Y-%m-%d")
            item['replytime']=date
            return item
        else:
           raise DropItem('最后回帖时间缺失:%s'%item)
